package com.server.questionbank.snframe.controller.Course.Chapter;

import com.server.questionbank.snframe.controller.Course.pojo.AddExercises;
import com.server.questionbank.snframe.controller.Course.pojo.Exercises.UpdateExercises;
import com.server.questionbank.snframe.domain.Homework.TExercises;
import com.server.questionbank.snframe.domain.ResDTO.PageResult;
import com.server.questionbank.snframe.domain.ResDTO.ResponseResult;
import com.server.questionbank.snframe.domain.User.TUser;
import com.server.questionbank.snframe.service.CourseService.*;
import com.server.questionbank.snframe.service.UsersService.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;

@RestController
@RequestMapping("/course/exercise")
public class CourseExercisesController {
    @Autowired
    private TExerciseService tExercisesService;
    @Autowired
    private TQuestionService tQuestionService;
    @Autowired
    private TKnowledgeService tKnowledgeService;
    @Autowired
    private TCourseService tCourseService;
    @Autowired
    private TChapterService tChapterService;
    @Autowired
    private UsersService usersService;

    // 获取章节习题列表
    @PostMapping("/getExercises")
    public ResponseResult<PageResult<TExercises>> getExercises(@RequestBody Map<String, Long> map) {
        PageResult<TExercises> exercisesList;
        try {
            if (map.get("knowledgeId") == null) // 没有知识点ID,用 章节ID
                exercisesList = tExercisesService.getExercisesByCourseId(map.get("courseId"), null,
                    null, null,
                    map.get("pageNum"), map.get("pageSize"));
            // 有知识点ID,用 知识点ID
            else exercisesList = tExercisesService.getExercisesByKnowledgeId(map.get("knowledgeId"),
                    null, null,
                    map.get("pageNum"), map.get("pageSize"));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error("获取章节习题列表失败!");
        }
        exercisesList.getList().forEach(exercises -> {
            exercises.setQuestions(tQuestionService.getQuestionByExercisesId(exercises.getExercisesId())); // 获取习题下的题目
            exercises.setKnowledges(tKnowledgeService.getKnowledgeByExercisesId(exercises.getExercisesId())); // 获取习题下的知识点
            exercises.setCourseInfo(tCourseService.getCourseById(exercises.getCourseId())); // 题目课程
            exercises.setChapterInfo(tChapterService.getChapterInfo(exercises.getChapterId())); // 题目章节
        });
        return ResponseResult.success(exercisesList);
    }

    // 获取习题详情
    @PostMapping("/getExercisesById")
    public ResponseResult<TExercises> getExercisesById(@RequestBody Map<String, Long> map) {
        if (map.get("exercisesId") == null) return ResponseResult.error("习题ID不能为空!");
        TExercises exercises = tExercisesService.getExercisesById(map.get("exercisesId"));
        exercises.setChapterInfo(tChapterService.getChapterInfo(exercises.getChapterId()));
        exercises.setQuestions(tQuestionService.getQuestionByExercisesId(map.get("exercisesId"))); // 获取习题下的题目
        exercises.setKnowledges(tKnowledgeService.getKnowledgeByExercisesId(map.get("exercisesId"))); // 获取习题下的知识点
        return ResponseResult.success(exercises);
    }

    // 添加习题
    @PostMapping("/addExercises")
    public ResponseResult<Long> addExercises(@RequestBody AddExercises addExercies)
    {
        TUser user = usersService.getLoginUser();
        if (user == null) return ResponseResult.error("未登录");
        // 添加习题数据
        tExercisesService.addExercises(addExercies);
        // 绑定知识点
        try
        {
            tExercisesService.addKnowledge(addExercies.getExercisesId(), addExercies.getKnowledges());
        } catch (Exception e) { e.printStackTrace(); }

        return ResponseResult.success(addExercies.getExercisesId());
    }

    @PostMapping("/updateExercises")
    public ResponseResult<Boolean> updateExercises(@RequestBody UpdateExercises updateExercises)
    {
        TUser user = usersService.getLoginUser();
        if (user == null) return ResponseResult.error("未登录");
        System.out.println(updateExercises);
        updateExercises.setUpdateBy(user.getUserId()); // 更新人
        if (!tExercisesService.updateExercises(updateExercises)) return ResponseResult.error("更新习题失败!");
        return ResponseResult.success(true);
    }
}
